﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ScrappyDB.Linq;

namespace ScrappyDB.IntegrationTests.AwsQuery101Examples
{
    [TestFixture]
    public class SortingTests
    {
/************************************************************************************
* Example SQL:          select * from mydomain where Year < '1980' order by Year asc 
* Description:          Retrieves all items released before 1980 and lists them in ascending order.	
* Expected result:      0802131786, 0385333498, 1579124585 
************************************************************************************/
        [Test]
        public void OrderByAscendingTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.YearInt < 1980 orderby a.YearInt ascending select a;

            Assert.AreEqual(3, all.ToList().Count());
            Assert.AreEqual("0802131786", all.ToList().First().Id);
        }

/************************************************************************************
* Example SQL:          select * from mydomain where Year < '1980' order by Year 
* Description:          Same as the previous entry, with "asc" (ascending) omitted.	
* Expected result:      0802131786, 0385333498, 1579124585 
************************************************************************************/
        [Test]
        public void OrderByTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.YearInt < 1980 orderby a.YearInt select a;

            Assert.AreEqual(3, all.ToList().Count());
            Assert.AreEqual("0802131786", all.ToList().First().Id);
        }

/************************************************************************************
* Example SQL:          select * from mydomain where Year = '2007' intersection Author is not null order by Author desc 
* Description:          Retrieves all items released in 2007 and sorts them by author name in descending order.	
* Expected result:      B00005JPLW, B000T9886K 
************************************************************************************/
        [Test]
        public void OrderByDescendingTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.YearInt == 2007 && a.Author.IsNotNull() orderby a.Author descending select a;

            Assert.AreEqual(2, all.ToList().Count());
            Assert.AreEqual("B00005JPLW", all.ToList().First().Id);
        }

/************************************************************************************
* Example SQL:          select * from mydomain order by Year asc 
* Description:          Invalid because Year is not constrained by a predicate in the where clause.	
* Expected result:      400 Error 
************************************************************************************/

/************************************************************************************
* Example SQL:          select * from mydomain where Year < '1980' order by Year limit 2 
* Description:          Retrieves two items that were released before 1980 and lists them in ascending order.	
* Expected result:      0802131786, 0385333498 
************************************************************************************/


    }
}
